package org.example.seata.common.feign.config;

import cn.hutool.core.util.StrUtil;
import feign.RequestInterceptor;
import feign.RequestTemplate;
import io.seata.core.context.RootContext;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.context.annotation.Configuration;

/**
 * 微服务之间调用传递SEATA全局事务ID
 *
 * @author hzq
 * @date 2021/11/30 17:43
 */
@Configuration
@ConditionalOnClass(RootContext.class)
public class FeignSeataRequestInterceptor implements RequestInterceptor {

    @Override
    public void apply(RequestTemplate template) {
        String xid = RootContext.getXID();
        if (StrUtil.isNotBlank(xid)) {
            template.header(RootContext.KEY_XID, xid);
        }
    }
}
